﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SuperJ.CMS.Interfaces.Repositories;
using SuperJ.CMS.Models;
namespace SuperJ.CMS.Repositories
{
    public class AdminMenuRepository : IAdminMenuRepository
    {
        public List<AdminMenuRoot> GetAdminMenus()
        {

            string xmlPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/AdminMenu.xml");
            //XmlTextReader xtr=new XmlTextReader(xmlPath);
            DataSet ds = new DataSet();
            ds.ReadXml(xmlPath);
            List<AdminMenuRoot> adminMenuRoots = new List<AdminMenuRoot>();
            int i = 0;
            foreach (DataRow rrow in ds.Tables[0].Rows)
            {

                AdminMenuRoot amrm = new AdminMenuRoot();
                amrm.Title = rrow["Title"].ToString();
                amrm.IsOpen = string.IsNullOrEmpty(rrow["IsOpen"].ToString()) ? false : Convert.ToBoolean(rrow["IsOpen"]);
                amrm.AdminMenuItems = new List<AdminMenuItem>();
                foreach (DataRow irow in ds.Tables[1].Select("root_id=" + i.ToString()))
                {
                    AdminMenuItem amim = new AdminMenuItem();
                    amim.Title = irow["item_Text"].ToString();
                    amim.Action = irow["Action"].ToString();
                    amim.Controller = irow["Controller"].ToString();
                    amim.Power = string.IsNullOrEmpty(irow["Power"].ToString()) ? 0 : Convert.ToInt32(irow["Power"]);
                    amrm.AdminMenuItems.Add(amim);
                }
                adminMenuRoots.Add(amrm);
                i++;

            }
            return adminMenuRoots;
        }
    }
}
